package com.freak.freakmusic.utils

import android.content.Context
import android.view.Gravity
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.freak.freakmusic.R
import com.freak.freakmusic.app.App



/**
 *
 * @author freak
 * @date 2019/9/11.
 */
class ToastUtil(protected var context: Context) {
    private var toast: Toast? = null
    private var message: String? = null
    private var mTextViewMessage: TextView? = null

    fun show() {
        if (toast != null) {
            toast!!.show()
        }
    }

    fun setText(text: String) {
        message = text
    }

    fun create(): Toast {

        if (toast == null) {
            val contentView = View.inflate(context, R.layout.view_dialog_toast, null)
            mTextViewMessage = contentView.findViewById(R.id.tv_toast_msg) as TextView
            toast = Toast(context)
            toast!!.view = contentView
            toast!!.setGravity(Gravity.TOP, 0, 800)
            toast!!.duration = Toast.LENGTH_LONG
            mTextViewMessage!!.text = message
        } else {
            mTextViewMessage!!.text = message
        }

        return toast as Toast
    }

    /**
     * 不管触发多少次toast调用，只会持续一次toast显示时长
     *
     * @return
     */
    fun createShort(): Toast {

        if (toast == null) {
            val contentView = View.inflate(context, R.layout.view_dialog_toast, null)
            mTextViewMessage = contentView.findViewById(R.id.tv_toast_msg) as TextView
            toast = Toast(context)
            toast!!.view = contentView
            toast!!.setGravity(Gravity.TOP, 0, 800)
            toast!!.duration = Toast.LENGTH_SHORT
            mTextViewMessage!!.text = message
        } else {
            mTextViewMessage!!.text = message
        }
        return toast as Toast
    }

    companion object {

        var isShow = true
        private var mToast: Toast? = null
        private var timeStamp: Long = 0
        private val TIME: Long = 3000
        private var sToastUtil: ToastUtil? = null

        fun show(resId: Int) {
            show(App.instance!!.getString(resId))
        }

        fun show(msg: String) {
            if (sToastUtil == null) {
                sToastUtil = ToastUtil(App.instance!!)
            }
            sToastUtil!!.setText(msg)
            sToastUtil!!.create().show()
        }

        fun shortShow(msg: String) {
            if (sToastUtil == null) {
                sToastUtil = ToastUtil(App.instance!!)
            }
            sToastUtil!!.setText(msg)
            sToastUtil!!.createShort().show()
        }


        fun showLong(context: Context, messgae: CharSequence) {
            try {
                if (isShow) {
                    toToast(context, messgae, Toast.LENGTH_SHORT)
                }
            } catch (e: Exception) {

            }

        }


        fun showShort(context: Context, message: CharSequence) {
            try {
                if (isShow) {
                    toToast(context, message, Toast.LENGTH_SHORT)
                }
            } catch (e: Exception) {

            }

        }


        fun showNetErroMsg(context: Context, e: Throwable) {
            try {
                toToast(context, e.message, Toast.LENGTH_SHORT)
            } catch (e1: Exception) {

            }

        }

        private fun toToast(context: Context, content: CharSequence?, time: Int) {
            //3秒内不会重复弹出
            if (System.currentTimeMillis() - timeStamp > TIME) {
                mToast = null
            }
            timeStamp = System.currentTimeMillis()
            if (mToast != null) {
                mToast!!.setText(content)
            } else {
                mToast = Toast.makeText(context, content, time)
            }
            mToast!!.show()
        }
    }


}
